using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace MobileStreams
{
    public partial class frmOverviewChannels : Form
    {
        // Index of selected Category
        private int iCategory;

        // Specifies is Audio or Video channels are displayed
        private ViewTypes currentType;

        // Array of all Channels currently viewable
        private Channel[] currentChannels;

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="pCategory">999 = Custom Channel</param>
        /// <param name="pVTyp"></param>
        public frmOverviewChannels(int pCategory, int pVTyp)
        {
            InitializeComponent();

            iCategory = pCategory;
            if (pVTyp == 0)
            {
                currentType = ViewTypes.Audio;
            }
            else
            {
                currentType = ViewTypes.Video;
            }
        }

        private void frmOverviewChannels_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            ListChannels();

            if (iCategory == 999)
            {
                this.Menu = mainMenuCustom;
            }

            Cursor.Current = Cursors.Default;
        }



        /// <summary>
        /// Loads all Channels in "currentChannels" into the Listview "lvChannels"
        /// </summary>
        /// <param name="Category">Category-Key (Index)</param>
        private void ListChannels()
        {
            ImageList imgLstChanPics = new ImageList();
            imgLstChanPics.ImageSize = new Size(57, 57);

            if (iCategory == 999)
            {
                currentChannels = XMLHandling.getChannels(XMLHandling.strAppDir + "\\CustomContent.xml");
            }
            else
            {
                if (currentType == ViewTypes.Video)
                {
                    currentChannels = XMLHandling.getChannels(XMLHandling.strAppDir + "\\VideoContent.xml");
                    //XMLHandling.WriteChannels(currentChannels, XMLHandling.strAppDir + "\\VideoContent.xml");
                }
                else
                {
                    currentChannels = XMLHandling.getChannels(XMLHandling.strAppDir + "\\AudioContent.xml");
                    //XMLHandling.WriteChannels(currentChannels, XMLHandling.strAppDir + "\\AudioContent.xml");
                }
            }

            // Fetch through all available channels
            int i = 0;
            foreach (Channel chan in currentChannels)
            {
                if (chan != null)
                {
                    if (chan.Category == iCategory || iCategory == 999)
                    {
                        ListViewItem lvi = new ListViewItem(chan.ChannelName);
                        lvi.ImageIndex = i;

                        if (File.Exists(XMLHandling.strAppDir + "\\ChanPics\\" + chan.ChannelLogo))
                        {
                            imgLstChanPics.Images.Add(new Bitmap(XMLHandling.strAppDir + "\\ChanPics\\" + chan.ChannelLogo));
                        }
                        else
                        {
                            imgLstChanPics.Images.Add(new Bitmap(64, 64));
                        }


                        lvi.Tag = chan;
                        lvChannels.Items.Add(lvi);

                        i++;
                    }
                }
            }

            lvChannels.LargeImageList = imgLstChanPics;

            //TODO: Select first ITEM
            //if (lvChannels.Items.Count > 0) lvChannels. = lvChannels.Items[0];
        }

        /// <summary>
        /// Button to display Channel-Details
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuitemSelect_Click(object sender, EventArgs e)
        {
            if (lvChannels.FocusedItem != null)
            {
                frmChannelDetails frm = new frmChannelDetails((Channel)lvChannels.FocusedItem.Tag);
                frm.ShowDialog();
            }
        }

        private void menuitemBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lvChannels_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                menuitemSelect_Click(null, null);
            }
        }

        private void menuitemSelectC_Click(object sender, EventArgs e)
        {
            // Do exactly the same that the other Select-Button does
            menuitemSelect_Click(sender, e);
        }

        private void menuitemNewchannel_Click(object sender, EventArgs e)
        {
            frmNewEditChannel frm = new frmNewEditChannel(null);
            frm.ShowDialog();

            // Refresh Listview to see changes
            lvChannels.Clear();
            ListChannels();
        }

        private void menuitemEditChannel_Click(object sender, EventArgs e)
        {
            if (lvChannels.FocusedItem != null)
            {
                frmNewEditChannel frm = new frmNewEditChannel((Channel)lvChannels.FocusedItem.Tag);
                frm.ShowDialog();

                // Refresh Listview to see changes
                lvChannels.Clear();
                ListChannels();
            }
            else
            { MessageBox.Show("Please select a channel"); }
        }

        private void menuitemCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuitemDelete_Click(object sender, EventArgs e)
        {
            if (lvChannels.FocusedItem != null)
            {
                Channel deleteChan = (Channel)lvChannels.FocusedItem.Tag;

                for (int i = 0; i < currentChannels.Length - 1; i++)
                {
                    bool foundChan = false;

                    if (currentChannels[i] == deleteChan)
                    {
                        foundChan = true;
                    }

                    if (foundChan)
                    {
                        currentChannels[i] = currentChannels[i + 1];
                    }
                }

                Channel[] newchannels = new Channel[currentChannels.Length - 1];

                for(int i = 0; i < currentChannels.Length - 1; i++)
                {
                    newchannels[i] = currentChannels[i];
                }

                XMLHandling.WriteChannels(newchannels, XMLHandling.strAppDir + "\\CustomContent.xml");

                // Refresh Listview to see changes
                lvChannels.Clear();
                ListChannels();
            }
            else
            { MessageBox.Show("Please select a channel");  }
        }

        private void menuitemSort_Click(object sender, EventArgs e)
        {
            XMLHandling.SortChannels(ref currentChannels);
            XMLHandling.WriteChannels(currentChannels, XMLHandling.strAppDir + "\\CustomContent.xml");
            
            // Refresh Listview to see changes
            lvChannels.Clear();
            ListChannels();
        }
    }
}